package com.example.demo.service;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import com.example.demo.auth.AuthenticationKit;
import com.jfinal.kit.HttpKit;
import com.jfinal.qyweixin.sdk.api.ApiResult;

public class HttpClientKit {
	public static final HttpClientKit me = new HttpClientKit();

	public String api_host ;//= "http://220.163.122.58:8081/ComplainMgmt";

	public String appid ;//= "CallManagerCenter";
	public String app_secret ;//= "0bce9fdb2b36b62281dd0e18cf80a5b6ce96a5d444adf8541300ac66";

	
	public void init(String api_host, String app_secret, String appid){
		System.out.println("初始化参数");
		setApi_host(api_host);
		setApp_secret(app_secret);
		setAppid(appid);
	}
	
	public String httpClientPostTest(String url, String app_secret, String json)
			throws Exception, UnsupportedEncodingException {
		String apiSignUrl = AuthenticationKit.getSignUrl(url, appid);
		String sign = AuthenticationKit.signUrlEncode(apiSignUrl, app_secret);
		String apiUrl = api_host + apiSignUrl + "&sign="
				+ URLEncoder.encode(sign, "UTF-8");

		System.out.println("sign: " + sign);// lpYLKc4T+x3o8mS5VtFwucDtfsQ=
		System.out.println("api访问路径: "+ apiUrl);
		System.out.println("api请求参数: " + json);
		Map<String, String> headers =  new HashMap<String, String>();
		headers.put("Content-Type", "application/json");
		// headers.put("Content-Type", "application/x-www-form-urlencoded");
		String result = HttpKit.post(apiUrl, json, headers);
		System.out.println("请求头: "+headers);
		// String userInfo2 =HttpUtils.post(apiUrl, r.toJson());
		System.out.println("++++++++++++++++++返回数据++++++++++++++++++++++");
		return result;
	}

	public String httpClientGetTokenTest() throws Exception {
		String tokenSignUrl = AuthenticationKit.getSignUrl("/token", appid);
		String tokenSign = AuthenticationKit.signUrlEncode(tokenSignUrl,
				app_secret);
		String getTokenRrl = api_host + tokenSignUrl + "&sign="
				+ URLEncoder.encode(tokenSign, "UTF-8");
		;
		System.out.println("获取token的路劲: " + getTokenRrl);

		String tokenResult = HttpKit.get(getTokenRrl);

		System.out.println("获取token返回值: " + tokenResult);
		ApiResult apiResult = new ApiResult(tokenResult);
		if(apiResult.isSucceed()) {
			System.out.println("=============================================");
			return (String) apiResult.getMap("data").get("token");
		}
		return null;
	}
	
	public String httpClientGetTokenTest(String url,String token, Map<String, String> queryParas) throws Exception {
		String tokenSignUrl = AuthenticationKit.getSignUrl(url, appid, queryParas);
		String tokenSign = AuthenticationKit.signUrlEncode(tokenSignUrl,
				token);
		String getTokenRrl = api_host + tokenSignUrl + "&sign="
				+ URLEncoder.encode(tokenSign, "UTF-8");
		System.out.println("获取token的路劲: " + getTokenRrl);
		String tokenResult = HttpKit.get(getTokenRrl);
		System.out.println("=============================================");
		return tokenResult;
	}

	public String getApi_host() {
		return api_host;
	}

	public void setApi_host(String api_host) {
		this.api_host = api_host;
	}

	public String getAppid() {
		return appid;
	}

	public void setAppid(String appid) {
		this.appid = appid;
	}

	public String getApp_secret() {
		return app_secret;
	}

	public void setApp_secret(String app_secret) {
		this.app_secret = app_secret;
	}
	
	
}
